package com.hx0311.pas.security.usertype.webapp;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.hx0311.core.dao.support.ResultSetUtil;
import com.hx0311.core.webapp.struts2.action.BaseAction;
import com.hx0311.pas.SysUtil;
import com.hx0311.pas.security.usertype.dao.UserTypeDao;
import com.hx0311.pas.security.usertype.model.UserType;

@SuppressWarnings({ "unchecked", "serial" })
public class UserTypeAction extends BaseAction<UserType, UserTypeDao> {

	private UserTypeDao dao;
	

	@Override
	public String edit() {
		if (StringUtils.isNotBlank(getModel().getRylb())) {
			Map data = getDao().get(getModel().getRylb());
			UserType type = new UserType();
			ResultSetUtil.copyMapValues(type, data);
			setModel(type);
		}
		return INPUT;
	}

	@Override
	public String index() {
		items = getDao().getAll();
		return SUCCESS;
	}
	
	@Override
	public String remove() {
		if (StringUtils.isNotBlank(getModel().getRylb())) {
			getDao().remove(getModel().getRylb());
		}
		return INDEX;
	}

	@Override
	public String save() {
		if (ADD.equals(getOptionType())) {
			getDao().add(getModel());
		} else {
			getDao().update(getModel());
		}
		SysUtil.loadRylb(getServletContext());
		return INDEX;
	}

	@Override
	public UserTypeDao getDao() {
		if (dao == null) {
			dao = new UserTypeDao();
		}
		return dao;
	}
}
